package com.beiye.drivertransport.SubActivity;

import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import com.alibaba.fastjson.JSON;
import com.beiye.drivertransport.R;
import com.beiye.drivertransport.activity.TwoBaseAty;
import com.beiye.drivertransport.bean.LearnphotoBean;
import com.beiye.drivertransport.config.AppInterfaceConfig;
import com.beiye.drivertransport.utils.TiShiDialog;
import com.githang.statusbar.StatusBarCompat;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.squareup.picasso.Picasso;

import java.util.List;


import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;
//违章学习查看学习照片
public class LearnphotoActivity extends TwoBaseAty {

     ImageView img_learnphotoback;

     GridView learn_gv;

     @Override
     public int getLayoutId() {
         return R.layout.activity_learnphoto;
     }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.img_learnphotoback) {
            finish();
        }

    }
     @Override
     public void initData() {
         img_learnphotoback = (ImageView) findViewById(R.id.img_learnphotoback);
         learn_gv = (GridView) findViewById(R.id.learn_gv);
         StatusBarCompat.setStatusBarColor(this, Color.parseColor("#1F80C4"));
     }

     @Override
     public void requestData() {

         Bundle bundle = getIntent().getExtras();
         int sn = bundle.getInt("sn");
         //违章学习学习照片
         OkGo.get(AppInterfaceConfig.BASE_URL+"course/userVtcPhoto/findByUvtSn/"+sn)
                 .tag(this)
                 .execute(new StringCallback() {
                     @Override
                     public void onSuccess(String result, Call call, Response response) {
                         LearnphotoBean learnphotoBean = JSON.parseObject(result, LearnphotoBean.class);
                         List<LearnphotoBean.RowsBean> rows = learnphotoBean.getRows();
                         if(rows==null)
                       {
                           return;
                       }
                         if(rows.size()==0)
                         {
                             return;
                         }
                         LearnImgAdapter learnImgAdapter = new LearnImgAdapter(LearnphotoActivity.this, rows);
                         learn_gv.setAdapter(learnImgAdapter);



                     }

                     @Override
                     public void onError(Call call, Response response, Exception e) {
                         super.onError(call, response, e);
                         String message = e.getMessage();
                         TiShiDialog.Builder builder1 = new TiShiDialog.Builder(LearnphotoActivity.this);
                         builder1.setMessage(message);
                         builder1.setTitle("提示:");
                         builder1.setPositiveButton("我知道", new DialogInterface.OnClickListener() {
                             public void onClick(DialogInterface dialog, int which) {
                                 dialog.dismiss();
                                 //设置你的操作事项
                             }
                         });

                         builder1.create().show();



                     }
                 });


     }
    public class LearnImgAdapter extends BaseAdapter {

        private Context mContext;

        /**
         * 每个分组下的每个子项的 GridView 数据集合
         */
        private List<LearnphotoBean.RowsBean> itemGridList;

        public LearnImgAdapter(Context mContext, List<LearnphotoBean.RowsBean> itemGridList) {
            this.mContext = mContext;
            this.itemGridList = itemGridList;
        }

        @Override
        public int getCount() {
            return itemGridList.size();
        }

        @Override
        public Object getItem(int position) {
            return itemGridList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (null == convertView) {
                convertView = View.inflate(mContext, R.layout.learnphoto_gridview_item, null);
            }
            ImageView img_gv = (ImageView) convertView.findViewById(R.id.img_twomainitem);
            String url = itemGridList.get(position).getUrl();

            Picasso.with(mContext).load(Uri.parse(url)).placeholder(R.mipmap.no_data).into(img_gv);
            return convertView;
        }
    }
 }
